package cn.sumpu.app.coolsk.biz.impl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
//import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sumpu.app.coolsk.biz.*;
import cn.sumpu.app.coolsk.constant.Constant;
import cn.sumpu.app.coolsk.constant.PageSize;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class ContentBizImpl implements ContentBiz {

//	private final static Logger logger = LoggerFactory
//			.getLogger(InfoBizImpl.class);

	@Autowired
	private IdentifierBiz identifierBiz;
	@Autowired
	private ChannelBiz channelBiz;
	@Autowired
	private DiscussBiz discussBiz;
	@Autowired
	private HotelRoomBiz hotelRoomBiz;
	@Autowired
	private TicketBiz ticketBiz;
	@Autowired
	private ProductBiz productBiz;
	private ContentDao contentDao;

	public void setContentDao(ContentDao contentDao) {
		this.contentDao = contentDao;
	}
	/**
	 * author BruceZhang
	 * @param channel
	 * @return 取得后台内容
	 * @throws AppBusinessException
	 */
	@Override
	public PageModel getBackContent(String channel, Integer pageSize, Integer page) throws AppBusinessException {
		if(page==null || page<1){  
			page = 1;
		}
		if(pageSize==null || pageSize<1){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		List<Content> contentList = contentDao.getBackContent(map);
		Integer count = getBackContentCount(channel);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(contentList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	/**
	 * author BruceZhang
	 * @param content
	 * 添加内容
	 * @throws AppBusinessException
	 */
	@Override
	public Content addContent(Content content) throws AppBusinessException {
		if(Common.emptyStr(content.getId())){
			content.setId(identifierBiz.generateId(Table.CONTENT));
		}
		if(content.getSort() == null){
			Integer minSort = getMinSortByChannel(content.getChannel());
			content.setSort(minSort-1);
		}
		contentDao.addContent(content);
		return content;
	}
	/**
	 * author BruceZhang
	 * @param id
	 * 删除内容
	 * @throws AppBusinessException
	 */
	@Override
	public void deleteContent(String id) throws AppBusinessException {
		contentDao.deleteContent(id);
	}
	/**
	 * author BruceZhang
	 * @param content
	 * 更新内容
	 * @throws AppBusinessException
	 */
	@Override
	public void updateContent(Content content) throws AppBusinessException {
		contentDao.updateContent(content);
	}
	/**
	 * author BruceZhang
	 * @param channel
	 * @return 取得后台内容
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getBackContentCount(String channel)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		return contentDao.getBackContentCount(map);
	}
	/**
	 * author BruceZhang
	 * @param map
	 * 根据条件查询存在多少条
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getContentExists(String channel, String tblName, String tblId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("tblName", tblName);
		map.put("tblId", tblId);
		return contentDao.getContentExists(map);
	}
	/**
	 * author BruceZhang
	 * @param id	内容ID
	 * @return	通过ID取得某条内容
	 * @throws AppBusinessException
	 */
	@Override
	public Content getContentById(String id) throws AppBusinessException {
		return contentDao.getContentById(id);
	}
	/**
	 * author BruceZhang
	 * @param channel	栏目标识
	 * @param number	条数
	 * @return	根据栏目取得内容(在前台展示)
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getContentByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		return contentDao.getContentByChannel(map);
	}
	//取得首页内容
	@Override
	public void getIndexContent(HttpServletRequest request) {
		try {
			//新闻专区的内容
			List<Content> newsContents = getContentByChannel("831b5456-7bad-4305-a181-491102d0b369", 6);
			request.setAttribute("newsContents", newsContents);
			//新闻专区的图
			List<Content> newsPics = getContentByChannel("f765cda7-74e6-4bbd-965e-30a16eeb2b88", 1);
			request.setAttribute("newsPics", newsPics);
			//轮播图
			List<Content> lunbo = getContentByChannel("18be5312-1483-496f-9b24-f1b2c5bbbef4", 4);
			request.setAttribute("lunbo", lunbo); 
			//头部广告位
			List<Content> alimamaHead = getContentByChannel("b0408691-138b-407a-a295-0823bf587dc1", 1);
			request.setAttribute("alimamaHead", alimamaHead);
			//中间广告位
			List<Content> alimamaMiddle = getContentByChannel("c6f62d9e-7092-4c1a-8057-56cc1f8c7ddd", 1);
			request.setAttribute("alimamaMiddle", alimamaMiddle);
			//三分位广告位
			List<Content> alimamaSan = getContentByChannel("1ba049f8-e5a4-4d68-892b-c8f6bfed92c7", 1);
			request.setAttribute("alimamaSan", alimamaSan);
			//底部广告位
			List<Content> alimamaFoot = getContentByChannel("de597e19-6a40-422e-9843-1cf5f55be7f2", 1);
			request.setAttribute("alimamaFoot", alimamaFoot);
			//国内游热推
			List<Content> guoNeiYou = getContentByChannel("b69fef3f-fef7-4451-9600-41371c23e594", 1);
			request.setAttribute("guoNeiYou", guoNeiYou);
			//主题
			List<Content> zhuTi = getContentByChannel("55f6ddc2-9ad8-4cad-b2c1-1c79a1ff903c", 1);
			request.setAttribute("zhuTi", zhuTi);
			//景点
			List<Content> jingDian = getContentByChannel("7aad1605-b716-483b-81ae-f77db87599b4", 1);
			request.setAttribute("jingDian", jingDian);
			//我的行程
			List<Content> woDeXingCheng = getContentByChannel("37d4ce40-95f0-4887-a761-bc712a436e29", 6);
			request.setAttribute("woDeXingCheng", woDeXingCheng);
			//今日推荐
			List<Content> jinRiTuiJian = getContentByChannel("d143611e-b671-4560-9f6e-07007db8d212", 6);
			getPrice(jinRiTuiJian);
			request.setAttribute("jinRiTuiJian", jinRiTuiJian);
			//打折酒店
			List<Content> daZheJiuDian = getHotelByChannel("3586c7ec-ffad-490c-a07d-156c935bb713", 8);
			request.setAttribute("daZheJiuDian", daZheJiuDian);
			//打折门票
			List<Content> daZheMenPiao = getTicketByChannel("66b7a85b-6886-40a9-9e62-139e2dea2069", 8);
			request.setAttribute("daZheMenPiao", daZheMenPiao);
			//特色美食
			List<Content> teSeMeiShi = getContentByChannel("93ddd4f6-5c1f-4914-ad41-27c2788818de", 5);
			request.setAttribute("teSeMeiShi", teSeMeiShi);
			//特色餐馆
			List<Content> teSeCanGuan = getContentByChannel("5ef659c4-9aa3-4cd5-84c3-0f76aec26456", 3);
			request.setAttribute("teSeCanGuan", teSeCanGuan);
			//旅游商品
			List<Content> lvYouShangPin = getProductByChannel("ff50dfce-a05c-4b60-9d81-83426b59d4e9", 5);
			request.setAttribute("lvYouShangPin", lvYouShangPin);
			//特价商品
			List<Content> teJiaShangPin = getContentByChannel("e2ee6ad4-02c8-40d1-9f6b-cd3102b8e0dc", 1);
			request.setAttribute("teJiaShangPin", teJiaShangPin);
			//热门景区排行
			List<Content> jingQuPaiHang = getContentByChannel("f9eb39f9-3672-4cb2-afe4-1e74901b2576", 5);
			request.setAttribute("jingQuPaiHang", jingQuPaiHang);
			//热门酒店排行
			List<Content> jiuDianPaiHang = getContentByChannel("554fecda-0759-40e6-9ed5-042128971c76", 5);
			request.setAttribute("jiuDianPaiHang", jiuDianPaiHang);
			//景区满意度排行
			List<Content> jingQuManYiDu = getContentByChannel("8e19272f-1a05-4e4b-85e0-9660d4770456", 5);
			getDiscount(jingQuManYiDu);
			request.setAttribute("jingQuManYiDu", jingQuManYiDu);
			//酒店满意度排行
			List<Content> jiuDianManYiDu = getContentByChannel("b9ce5fab-e3b9-42c9-9d9e-b981a3763fb2", 5);
			getDiscount(jiuDianManYiDu);
			request.setAttribute("jiuDianManYiDu", jiuDianManYiDu);
			//摄天下
			List<Content> sheTianXia = getContentByChannel("af67ea06-b61f-45aa-a24f-6c3ead35ae8a", 5);
			request.setAttribute("sheTianXia", sheTianXia);
			//农家乐
			List<Content> xiangCunKeZhan = getContentByChannel("84072914-aa10-4398-9b53-acb47c3b5f2b", 6);
			request.setAttribute("xiangCunKeZhan", xiangCunKeZhan);
			//精选线路
			List<Content> jingXuanLuXian = getProductByChannel("75a70305-8c97-46fa-9227-adf5dc44ab20", 18);
			request.setAttribute("jingXuanLuXian", jingXuanLuXian);
			//爽客热评
			List<Content> rePing = getDiscussByChannel("c9993029-3df9-4609-a294-e6f44a19e029", 4);
			request.setAttribute("rePing", rePing);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void getPrice(List<Content> jinRiTuiJian)
			throws AppBusinessException {
		for (Content content : jinRiTuiJian) {
			double priceMarket=0;
			double priceMin=0;
			if(content.getTblName().equals("coolsk_hotel")){
				Hotel hotel=hotelRoomBiz.getHotelById(content.getTblId());
				if(!Common.empty(hotel)){
					priceMarket=hotel.getMarketPrice();
					priceMin=hotel.getMinPrice();
				}
			}else if(content.getTblName().equals("coolsk_ticket")){
				Ticket ticket=ticketBiz.getTicketById(content.getTblId());
				if(!Common.empty(ticket)){
					priceMarket=ticket.getPriceMarket();
					priceMin=ticket.getPriceMin();
				}
			}else if(content.getTblName().equals("coolsk_product")){
				Product product=productBiz.getProductById(content.getTblId());
				if(!Common.empty(product)){
					priceMarket=product.getPriceMarket();
					priceMin=product.getPriceMin();
				}
			}
			content.setPriceMarket(priceMarket);
			content.setPriceMin(priceMin);
		}
	}
	//获取点评数
	private void getDiscount(List<Content> jingQuManYiDu)
			throws AppBusinessException {
		for (Content content : jingQuManYiDu) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("owner", content.getTblId());
			if(content.getTblName().equals("coolsk_hotel")){
				map.put("ownerType", "hotel");
			}else if(content.getTblName().equals("coolsk_scenic")){
				map.put("ownerType", "att");
			}
			int disCount=discussBiz.getDiscussCountByObj(map);
			content.setDisCount(disCount);
		}
	}
	/**
	 * BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return			查询推送的门票
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getTicketByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.TICKET);
		return contentDao.getTicketByChannel(map);
	}
	/**
	 * BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return			查询推送的酒店
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getHotelByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.HOTEL);
		return contentDao.getHotelByChannel(map);
	}
	/**
	 * BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return			查询推送的产品
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getProductByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.PRODUCT);
		return contentDao.getProductByChannel(map);
	}
	/**
	 * BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return			查询推送的景区
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getScenicByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.SCENIC);
		return contentDao.getScenicByChannel(map);
	}
	/**
	 * author BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return	查询推送的点评
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getDiscussByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.DISCUSS);
		return contentDao.getDiscussByChannel(map);
	}
	
	
	public PageModel getListViewContentByChannel(String channel, Integer pageSize,
			Integer page,boolean listAll) throws AppBusinessException {
		if(page==null || page<1){
			page = 1;
		}
		if(pageSize==null || pageSize<1){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		int count = contentDao.getContentByChannelCount(map);
		int startId = (page - 1) * pageSize;
		if(!listAll){
			Integer remove=Constant.CHANNEL_MAP.get(channel);
			remove=remove==null?0:remove;
			startId+=remove;
			count-=remove;
		}
		map.put("number", pageSize);
		map.put("startId", startId);
		List<Content> contentList = contentDao.getContentByChannel(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(contentList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	@Override
	public List<Content> getContentByChannel(String channel)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", Constant.CHANNEL_MAP.get(channel));
		return contentDao.getContentByChannel(map);
	}
	/**
	 * author BruceZhang
	 * @param channel	频道ID
	 * @param number	条数
	 * @return	查询推送的行程
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getTripByChannel(String channel, Integer number)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", channel);
		map.put("number", number);
		map.put("tblName", Table.TRIP);
		return contentDao.getTripByChannel(map);
	}
	/**
	 * author BruceZhang
	 * @param channel	频道号
	 * @return	通过频道查询最小的排序号
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getMinSortByChannel(String channel)
			throws AppBusinessException {
		Integer minSort = contentDao.getMinSortByChannel(channel);
		if(minSort == null){
			minSort = 2;
		}
		return minSort;
	}
	/**
	 * author BruceZhang
	 * @param type		1.上移 2.下移
	 * @param channel	频道号
	 * @param sort	当前序号
	 * @return	根据条件查询排序内容
	 * @throws AppBusinessException
	 */
	@Override
	public Content getSortByCondition(Integer type, String channel, Integer sort)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("channel", channel);
		map.put("sort", sort);
		return contentDao.getSortByCondition(map);
	}
	
	@Override
	public PageModel getListViewContentByChannel(String channel, Integer pageSize,
			Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		page=page==null?1:page;
		pageSize=pageSize==null?PageSize.PAGE_SIZE_SEARCH:pageSize;
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("channel", channel); 
		
		int count = contentDao.getContentCount(map);
		List<Content> list=contentDao.getContent(map);
		PageModel pm = new PageModel(); 
		pm.setCurrentPage(page);
		pm.setList(list);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	
	@Override
	public PageModel searchContent(Integer page,String channelType, String title)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		page=page==null?1:page;
		int size = PageSize.PAGE_SIZE_SEARCH;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		map.put("channels", getChannels(channelType)); 
		if(!Common.empty(title)){
			map.put("title", "'%"+title+"%'");
		}
		int count = contentDao.getContentCount(map); 
		List<Content> list=contentDao.getContent(map);
		PageModel pm = new PageModel(); 
		pm.setCurrentPage(page);
		pm.setList(list);
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}
	private String getChannels(String channelType) throws AppBusinessException{
		String parentChannel="";
		String channels="";
		if(!Common.empty(channelType)){
			if(channelType.equals("ticket")){
				parentChannel="48041e94-f65b-4ed4-bba6-e5bf1e38b4c4";
			}else if(channelType.equals("travel")){
				parentChannel= "18646979-84cf-4e4a-bbdf-f1e0f291085c";
			}else if(channelType.equals("line")){
				parentChannel= "8cc54913-8fc9-4e4f-9130-8832cd43ad68";
			}else if(channelType.equals("exhibition")){
				parentChannel="ab1c1459-862a-4134-bcea-82695426ae80";
			}
			List<Channel> list=new ArrayList<Channel>();
			Channel ch=channelBiz.getChannelByMark(parentChannel);
			List<Channel> listChannel=channelBiz.getChildChannelList(ch.getId(), list);
			int length=listChannel.size();
			for (int i = 0; i < length; i++) {
				String c=listChannel.get(i).getMark();
				if(i==0 && length==1){
					channels+="('"+c+"')";
				}else if(i==0 && length>1){
					channels+="('"+c+"',";
				}else if(i==length-1){
					channels+="'"+c+"')";
				}else{
					channels+="'"+c+"',";
				}
				
			}
		}
		return channels;
	}
}
